﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using SpreadsheetLight;
using UtilsLib.UtilsFile.CSV;
using UtilsLib.UtilsLog;

namespace UtilsLib.UtilsFile
{
    public static class WriteToFile
    {

        private static CsvFileWriter csvWriter;


        /// <summary>
        /// Write in the File the content of the fileObj.
        /// </summary>
        /// <param name="fileObj">The content of the file </param>
        /// <param name="filePath">The path of the file</param>
        /// <param name="name"> The name of the file</param>
        /// <param name="deleteIfExist"> Specify if you want to delete the old file or write after</param>
        /// <param name="separator"> OPTIONNAL : Specify the sperator for CSV file ( ";" , ",", ... )</param>
        public static void WriteCSV(FileObject fileObj, string filePath, string name, bool deleteIfExist, char separator=';')
        {
            
            try
            {
                csvWriter = new CsvFileWriter(FileLib.CreateFile(filePath, name, "csv", deleteIfExist));

                CsvFileWriter.separator = separator;

                CsvRow row = new CsvRow();
                row.AddRange(fileObj.ColumnsName.GetRow());
                csvWriter.WriteRow(row);

                foreach (FileRow frow in fileObj.Rows)
                {
                    row.Clear();
                    row.AddRange(frow.GetRow());
                    csvWriter.WriteRow(row);
                }
            }
            catch (Exception ex)
            {
                LogToFile.WriteException(ex, "Error Writing the CSV File");
            }
            finally
            {
                csvWriter.Close();
                csvWriter.Dispose();
            }
        }

        /// <summary>
        /// Create a XLSX File
        /// </summary>
        /// <param name="fileObjs">The liste of WorkSheets</param>
        /// <param name="filePath">The path of the file</param>
        /// <param name="name">The name of the file</param>
        public static void WriteXLSX(List<FileObject> fileObjs, string filePath, string name)
        {
            
            int wsCount = 1;
            int rowCount, ColCount;

            string pathToFile = FileLib.GetPath(filePath, name, "xlsx");

            FileLib.CheckFile(pathToFile,true);

            //pathToFile = FileLib.GetRelativePath(filePath, name, "xlsx");
            SLDocument doc = new SLDocument();

            try
            {

                foreach (FileObject fo in fileObjs)
                {
                    //Set a default name if there are no specified name.
                    fo.Name = fo.Name == "" ? ("Sheet_" + wsCount) : fo.Name;

                    //Rename the worksheet if this is the first sheet.
                    if (wsCount == 1)
                        doc.RenameWorksheet(SLDocument.DefaultFirstSheetName, fo.Name);
                    else
                        doc.AddWorksheet(fo.Name);

                    doc.SelectWorksheet(fo.Name);

                    rowCount = 1;
                    //Set the Column Name
                    if (fo.ColumnsName.Columns.Count != 0)
                    {
                        ColCount = 1;
                        foreach (string colValue in fo.ColumnsName.Columns)
                        {
                            doc.SetCellValue(rowCount, ColCount, colValue);
                            ColCount++;
                        }
                        rowCount++;
                    }
                    
                    //Set the Values
                    foreach (FileRow fr in fo.Rows)
                    {
                        ColCount = 1;
                        foreach (string colValue in fr.Columns)
                        {
                            doc.SetCellValue(rowCount, ColCount, colValue);
                            ColCount++;
                        }
                        rowCount++;
                    }

                    wsCount++;
                }

                doc.SaveAs(pathToFile);

            }
            catch (Exception ex)
            {
                LogToFile.WriteException(ex, "Error Writing the XLSX File");
                doc.CloseWithoutSaving();
            }
            finally
            {
                LogToFile.WriteMessage("End XLSX File");
            }
        }
    }
}
